package cn.donting.jme.core.state;

import cn.donting.jme.EditorContent;
import cn.donting.jme.JmeApplication;
import cn.donting.jme.ioc.Bean;
import com.jme3.app.Application;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Spatial;

/**
 * Camera lookAt Spatial
 */
@Bean
public class CameraLookAtState extends AbstractEditorAppState {

    private AppStateManager stateManager;
    private JmeApplication app;
    private float targetDistance=10f;

    @Override
    public void initialize(AppStateManager stateManager, JmeApplication app) {
        this.stateManager = stateManager;
        this.app = app;
    }

    public void lookAt(Camera camera, Spatial spatial) {
        EditorContent.runJme(() -> {
            camera.lookAt(spatial.getWorldTranslation(), Vector3f.UNIT_Y);
            Vector3f worldTranslation = spatial.getWorldTranslation();
            Vector3f direction = camera.getDirection();
            Vector3f camLocation = worldTranslation.add(direction.negate().mult(targetDistance));
            camera.setLocation(camLocation);
        });

    }

    @Override
    public void update(float tpf) {

    }
}
